#!/usr/bin/python

#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007  Brian Langenberger

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


import optparse,os.path
import audiotools

OUTPUT = "%(hours)d:%(minutes)2.2d:%(seconds)2.2d"

if (__name__ == '__main__'):
    parser = optparse.OptionParser(
        "%prog <track 1> [track 2] ...",
        version="Python Audio Tools %s" % (audiotools.VERSION))

    (options,args) = parser.parse_args()

    total_length = 0l

    audio_files = audiotools.open_files(filter(os.path.isfile,args))
    total_length += sum([file.cd_frames() for file in audio_files])

    for parent_dir in filter(os.path.isdir,args):
        for (dir,dirs,files) in os.walk(parent_dir):
            total_length += sum(
                [file.cd_frames() for file in
                 audiotools.open_files([os.path.join(dir,f) for f in files])])

    if (total_length > 0):
        print OUTPUT % {"hours":total_length / (75 * 60 * 60),
                        "minutes":total_length / (75 * 60) % 60,
                        "seconds":total_length / 75 % 60}
    else:
        print OUTPUT % {"hours":0,"minutes":0,"seconds":0}
